<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div class="header_right" @click="showCardNav = !showCardNav">
          <div class="title">搜索</div>
          <div>
            <div :class="{ 'arrowTransform': !showCardNav, 'arrowTransformReturn': showCardNav}">
              <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
            </div>
          </div>
        </div>
        <div v-show="showCardNav" class="hr"></div>
        <el-collapse-transition>
          <div v-show="showCardNav">
            <div class="search">
              <span style="display: inline-block;margin-bottom: 10px;">
                <span style="margin-right: 12px;">设备名称</span>
                <el-input v-model="tableParam.equipmentName" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">设备自编码</span>
                <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">所属单位</span>
                <el-input v-model="tableParam.company" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">站点名称</span>
                <el-input v-model="tableParam.stationName" style="width: 200px;" clearable placeholder="请输入" @keyup.enter.native="gettableData()" />
              </span>
              <span style="margin-left: 24px;">
                <span style="margin-right: 12px;">设备类型</span>
                <el-select v-model="tableParam.equipmentTypeId" placeholder="请选择">
                  <el-option v-for="item in equipmentTypelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </span>
              <el-button icon="el-icon-search" size="medium" class="bottom_all but1" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
              <el-button size="medium" class="bottom_all_two" style="margin-left:12px;" @click="init()">重 置</el-button>
            </div>
          </div>
        </el-collapse-transition>
        <div class="hr"></div>
        <div class="card">
          <div class="card_nav">
            <div>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="pagesaddFromDate()">
                <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                添加
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='RIYONG'))!==-1" size="small" class="bottom_three" @click="Update(1)">
                <i class="icon iconfont iconicon_button_startuse" style="font-size: 12px;"></i>
                启用
              </el-button>
              <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='JINYONG'))!==-1" size="small" class="bottom_four" @click="Update(0)">
                <i class="icon iconfont iconicon_button_disable1" style="font-size: 12px;"></i>
                禁用
              </el-button>
              <!-- <el-button size="small" class="botton_five" @click="showtype = true">泵类型维护</el-button> -->
            </div>
            <div>
              <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                <el-link :underline="false">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_filtrate"></i>
                  </span>
                  筛选
                </el-link>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in table" :key="index">
                    <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="load()">
                <span class="el-icon--left">
                  <i class="iconfont iconicon_button_export1"></i>
                </span>
                导出
              </el-link>
            </div>
          </div>
          <div v-loading="tableLoading" class="card_info" :style="style">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
              <el-table-column align="center" type="selection"></el-table-column>
              <template v-for="(item, index2) in table">
                <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :min-width="item.width" :fixed="item.label =='操作'?'right':false">
                  <template slot-scope="scope">
                    <span v-if="scope.row.enable == 0 && item.label == '状态'" style="color:#969799;">禁用</span>
                    <span v-if="scope.row.enable == 1 && item.label == '状态'" style="color:#00A754;">启用</span>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="pagesupdateFromDate(scope.row)">修改</el-button>
                    <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='SHEBEIWEIHU'))!==-1" class="bottom_one" size="mini" @click="maintenance(scope.row)">设备维护</el-button>
                    <span v-if="item.label !== '操作' && item.label !== '状态'">{{ scope.row[item.prop] }}</span>
                  </template>
                </el-table-column>
              </template>
            </el-table>
          </div>
          <!-- <div class="card_bottom">
            <div class="card_bottom_left">
              <div class="top_page_totle">总共{{ total }}条</div>
              <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
            </div>
            <div>
              <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
            </div>
          </div> -->
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="addDialog" width="480px" :close-on-click-modal="false" :before-close="resetFromDate">
      <span slot="title" class="dialog-title">{{ addDialogTitle }}</span>
      <div class="dialoginfo">
        <el-form ref="reffromDate" :rules="rules" :inline="true" :model="fromDate" label-width="110px">
          <el-form-item prop="equipmentName">
            <span slot="label" style="color: #FF0000 ;">设备名称</span>
            <el-input v-model="fromDate.equipmentName" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <span slot="label" style="color: #FF0000 ;">设备自编码</span>
            <el-input v-model="fromDate.code" placeholder="请输入" clearable @input="changecode('code')"></el-input>
          </el-form-item>
          <el-form-item prop="equipmentTypeId">
            <span slot="label" style="color: #FF0000 ;">设备类型</span>
            <el-select v-model="fromDate.equipmentTypeId" placeholder="请选择">
              <el-option v-for="item in equipmentTypelist" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item v-if="UserInfo.adminStatus == 1" prop="companyId">
            <span slot="label">所属单位</span>
            <el-select ref="selection1" v-model="fromDate.companyId" placeholder="请选择" @change="setstationIdlist(fromDate.companyId),$nextTick(()=>{fromDate.company = $refs.selection1.selectedLabel})">
              <el-option v-for="item in companylist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="stationId">
            <span slot="label">站点名称</span>
            <el-select ref="selection2" v-model="fromDate.stationId" placeholder="请选择" @change="getproline(),get_simple_organizationFun(fromDate.stationId),$nextTick(()=>{fromDate.stationName = $refs.selection2.selectedLabel})">
              <el-option v-for="item in stationIdlist" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="productLineId">
            <span slot="label">生产线</span>
            <el-select v-model="fromDate.productLineId" placeholder="请选择" :disabled="productLineIdList.length==0">
              <el-option v-for="item in productLineIdList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="enable">
            <span slot="label">状态</span>
            <el-radio-group v-model="fromDate.enable">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetFromDate">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="pagessubmitFromDate()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 设备维护 -->
    <el-dialog v-dialogDrag :visible.sync="maintenanceDialog" width="1072px" :close-on-click-modal="false" :before-close="resetOtherFromDate">
      <span slot="title" class="dialog-title">设备维护</span>
      <div class="dialogOtherinfo">
        <div class="nav-one" style="min-height: 530px;">
          <div class="navone-nav">
            <template v-for="(item, index) in navlist">
              <div :key="index" :class="navindex == item.id ? 'active' : ''" @click="navindex = item.id">{{ item.name }}</div>
            </template>
          </div>
          <div v-if="navindex == 1" class="navone-info">
            <el-form :inline="true" label-width="110px">
              <el-form-item>
                <span slot="label">故障类型</span>
                <el-select v-model="maintenanceData.faultTypeId" placeholder="请选择">
                  <el-option v-for="item in MaintenanceType" :key="item.id" :label="item.itemName" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <span slot="label">维修时间</span>
                <el-date-picker v-model="maintenanceData.maintainTime" type="datetime" placeholder="选择日期时间" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
              </el-form-item>
              <el-form-item>
                <span slot="label">备 注</span>
                <el-input v-model="maintenanceData.remark" placeholder="请输入" class="textarea" type="textarea" resize="none" rows="10" clearable></el-input>
              </el-form-item>
            </el-form>
          </div>
          <div v-if="navindex == 2" class="navone-info">
            <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="maintenancelist" tooltip-effect="dark">
              <el-table-column label="序号" width="50" align="center">
                <template slot-scope="scope">{{scope.$index +1}}</template>
              </el-table-column>
              <el-table-column prop="faultTypeName" label="故障类型" width="180" align="center"></el-table-column>
              <el-table-column prop="maintainTime" label="维修时间" width="180" align="center"></el-table-column>
              <el-table-column prop="remark" label="备注" width="auto" align="center"></el-table-column>
              <el-table-column prop="updateTime" label="更新时间" width="180" align="center"></el-table-column>
            </el-table>
            <el-pagination :current-page.sync="maintenanceParam.page" :page-size="maintenanceParam.pageSize" layout="prev, pager, next, jumper" :total="maintenancetotal" @current-change="getmaintenancelist()"></el-pagination>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resetOtherFromDate">取 消</el-button>
        <el-button v-if="navindex == 1" class="bottom_all" style="margin-left: 24px;" @click="submitmaintenance()">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage,
  batchUpdateOrDelete,
  updateSortNum,
  saveOrUpdate,
  Download,
  equipmentMaintainQueryPagee,
  equipmentMaintainsave,
} from "@/api/basicConfig/otherEquipmente.js";
import { getPLByStationId } from "@/api/basicConfig/site.js";
import { getDicType, getcompany, getUserAutList, get_simple_organization } from "@/api/common.js";
import { getUserInfo } from "@/utils/auth.js";
import { get_station_list } from "@/api/smart/stocksake.js";
export default {
  mixins: [mixin],
  data() {
    return {
      UserInfo: '',
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      batchUpdateOrDelete: batchUpdateOrDelete,
      updateSortNum: updateSortNum,
      saveOrUpdate: saveOrUpdate,
      DownloadURL: Download,
      /*表格数据*/
      table: [
        {
          label: "设备自编码",
          width: "140",
          prop: "code",
          status: true,
        },
        {
          label: "所属单位",
          width: "120",
          prop: "company",
          status: true,
        },
        {
          label: "站点名称",
          width: "120",
          prop: "stationName",
          status: true,
        },
        {
          label: "生产线",
          width: "120",
          prop: "productLineName",
          status: true,
        },
        {
          label: "设备名称",
          width: "120",
          prop: "equipmentName",
          status: true,
        },
        {
          label: "设备类型",
          width: "120",
          prop: "equipmentTypeName",
          status: true,
        },
        {
          label: "状态",
          width: "80",
          prop: "useMode",
          status: true,
        },
        {
          label: "更新时间",
          width: "160",
          prop: "updateTime",
          status: true,
        },
        {
          label: "操作",
          width: "180",
          prop: "",
          status: true,
        },
      ],
      /*表单页类型，0为无树状，1为有树状，有树状在el-main加上display:flex*/
      pagesType: 0,
      /*添加表单数据*/
      rules: {
        // plateNumbers:[
        // 	{ validator: validatePass, trigger: 'blur' }
        // ],
        code: [
          {
            required: true,
            message: "请输入设备自编码",
            trigger: "blur",
          },
        ],
        stationId: [
          {
            required: true,
            message: "请选择站点",
            trigger: "change",
          },
        ],
        productLineId: [
          {
            required: true,
            message: "请选择生产线",
            trigger: "change",
          },
        ],
        typeId: [
          {
            required: true,
            message: "请选择设备类型",
            trigger: "change",
          },
        ],
        branchId: [
          {
            required: true,
            message: "请选择单位",
            trigger: "change",
          },
        ],
      },
      fromDate: {
        code: "",
        stationName: "",
        stationId: "",
        equipmentName: "",
        equipmentTypeName: "",
        productLineId: "",
        productLineName: "",
        equipmentTypeId: "",
        departmentName: "",
        companyId: "",
        enable: 1,
        type: 2,
        company: '',
      },
      companylist: [],
      equipmentTypelist: [],
      productLineIdList: [],
      maintenanceDialog: false,
      navlist: [
        {
          name: "录入维修结果",
          id: 1,
        },
        {
          name: "设备维修记录",
          id: 2,
        },
      ],
      maintenanceParam: {
        page: 1,
        pageSize: 9,
      },
      maintenanceData: {
        faultTypeName: "",
        faultTypeId: "",
        maintainTime: "",
        remark: "",
      },
      maintenancelist: [],
      maintenancetotal: "",
      navindex: 1,
      stationIdlist: [],
      showtype: false,
      typelist: [],
      MaintenanceType: [],
    };
  },
  async created() {
    this.UserInfo = await getUserInfo();
    if (this.UserInfo.adminStatus == 1) {
      /**通过typeId为公司类型id查询所有公司 */
      getcompany({ typeId: "19da56b9e85648059e3cf1e7da497da4" }).then((res) => {
        this.companylist = res.data;
      });
    } else {
      this.stationIdlist = await getUserAutList(true)
    }
    this.tableParam = { ...this.tableParam, code: "", equipmentName: "", type: 2 };
    this.gettableData();
    this.gettypeList();
    /**通过typeId为公司类型id查询所有公司 */
    getcompany({ typeId: "19da56b9e85648059e3cf1e7da497da4" }).then((res) => {
      this.companylist = res.data;
    });
    getDicType({ code: "MaintenanceType" }).then((res) => {
      this.MaintenanceType = res.data.items;
    });
  },
  /*
   *copy(data)深拷贝数据,返回拷贝完的数据
   *gettableData获取列表数据
   *load下载
   *SortNum保持序列号
   * UpdateOrDelete禁用，启用，删除
   * submitFromDate保存更新数据
   */
  methods: {
    /**通过站点查询公司 */
    get_simple_organizationFun(id) {
      get_simple_organization({ id: id }).then((res) => {
        this.fromDate.companyId = res.data.id
        this.fromDate.company = res.data.name
      });//查询站点所属公司
    },
    /*根据公司获取站点*/
    setstationIdlist() {
      if (this.fromDate.companyId) {
        get_station_list({ parentId: this.fromDate.companyId }).then((res) => {
          this.stationIdlist = res.data;
          this.getproline()
        });
      }
    },
    /*获取生产线*/
    getproline() {
      getPLByStationId({ stationId: this.fromDate.stationId }).then((res) => {
        this.productLineIdList = res.data;
        this.fromDate.productLineId = "";
      });
    },
    pagesupdateFromDate(data) {
      getPLByStationId({ stationId: data.stationId }).then((res) => {
        this.productLineIdList = res.data;
        this.updateFromDate(data, "修改");
      });
    },
    pagesaddFromDate() {
      this.productLineIdList = [];
      this.addFromDate("添加");
    },
    gettypeList() {
      getDicType({ code: "otherSEType" }).then((res) => {
        this.equipmentTypelist = res.data.items;
      });
    },
    pagessubmitFromDate() {
      let _this = this;
      _this.$refs.reffromDate.validate((valid) => {
        if (valid) {
          _this.fromDate.productLineName = _this.$fromatTable(
            this.productLineIdList,
            _this.fromDate.productLineId,
            "id",
            "name"
          );
          _this.fromDate.equipmentTypeName = _this.$fromatTable(
            this.equipmentTypelist,
            _this.fromDate.equipmentTypeId,
            "id",
            "itemName"
          );
          _this.submitFromDate();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    maintenance(low) {
      this.id = low.id;
      this.maintenanceDialog = true;
      this.maintenanceData = this.$options.data().maintenanceData;
      this.getmaintenancelist();
    },
    getmaintenancelist() {
      equipmentMaintainQueryPagee({
        ...this.maintenanceParam,
        equipmentId: this.id,
      }).then((res) => {
        this.maintenancetotal = res.data.totalRows;
        this.maintenancelist = res.data.data;
      });
    },
    submitmaintenance() {
      this.maintenanceData.faultTypeName = this.MaintenanceType.find(
        (res) => res.id == this.maintenanceData.faultTypeId
      ).itemName;
      equipmentMaintainsave({
        ...this.maintenanceData,
        equipmentId: this.id,
      }).then((res) => {
        if (res.code * 1 === 1001) {
          this.$message.success('记录更新成功！！！')
          this.resetOtherFromDate();
        }
      });
    },
    resetOtherFromDate() {
      this.maintenanceDialog = false;
    },
    init() {
      /*重置数据*/
      this.tableParam.equipmentName = "";
      this.tableParam.code = "";
      this.gettableData();
    },
  },
};
</script>

<style lang="scss" scoped>
.checkcycdialoginfo {
  .el-input {
    width: 100px;
  }
  .el-select {
    width: 100px;
    margin-right: 20px;
  }
  .text {
    padding: 10px;
  }
}
.dialogOtherinfo {
  .el-pagination {
    padding: 10px 0;
  }
  min-height: 530px;
  .textarea {
    width: 870px;
  }

  .el-input,
  .el-select {
    width: 375px;
  }
}

.table_link {
  margin-left: 10px;
  color: $bg_color;
}

.table_link:hover {
  opacity: 0.8;
}

.el-input,
.el-select {
  width: 240px;
}
</style>
